package com.manager.service.impl;

import com.manager.bean.ClassInfo;
import com.manager.bean.ClassInfoExample;
import com.manager.bean.PageInfo;
import com.manager.mapper.ClassInfoMapper;
import com.manager.service.ClassInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Classinfo)表服务实现类
 *
 * @author makejava
 * @since 2021-03-02 11:38:32
 */
@Service("classinfoService")
public class ClassInfoServiceImpl implements ClassInfoService {
    @Resource
    private ClassInfoMapper classinfoDao;

    @Override
    public PageInfo<ClassInfo> findPageInfo(Integer Id, String Name, String Counsellor, String Phone, Integer pageIndex, Integer pageSize) {
        PageInfo<ClassInfo> pi = new PageInfo<ClassInfo>();
        pi.setPageIndex(pageIndex);
        pi.setPageSize(pageSize);
        ClassInfoExample classInfoExample=new ClassInfoExample();
        ClassInfoExample.Criteria criteria=classInfoExample.createCriteria();
        if(Name!=null&&Name!=""){
            criteria.andNameLike("%"+Name+"%");
        }
        if(Id!=null){
            criteria.andIdEqualTo(Id);
        }
        if(Counsellor!=null&&Counsellor!=""){
            criteria.andCounsellorEqualTo(Counsellor);
        }
        if(Phone!=null&&Phone!=""){
            criteria.andCounsellorPhoneEqualTo(Phone);
        }
        //获取总条数
        Integer totalCount = classinfoDao.countByExample(classInfoExample);
        if (totalCount>0){
            pi.setTotalCount(totalCount);
            //每一页显示学生信息数
            //currentPage = (pageIndex-1)*pageSize  当前页码数减1*最大条数=开始行数
            List<ClassInfo> classInfos =	classinfoDao.selectByExample(classInfoExample);
            int start=(pi.getPageIndex()-1)*pi.getPageSize();
            int end=start+pi.getPageSize();
            if(end>classInfos.size()-1){
                end=classInfos.size();
            }
            classInfos=classInfos.subList(start,end);
            pi.setList(classInfos);
        }
        return pi;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ClassInfo queryById(Integer id) {
        return this.classinfoDao.selectByPrimaryKey(id);
    }

    @Override
    public List<ClassInfo>  getAll(){
        ClassInfoExample classInfoExample = new ClassInfoExample();
        ClassInfoExample.Criteria criteria = classInfoExample.createCriteria();
        return classinfoDao.selectByExample(classInfoExample);
    }

    /**
     * 新增数据
     *
     * @param classinfo 实例对象
     * @return 实例对象
     */
    @Override
    public ClassInfo insert(ClassInfo classinfo) {
        this.classinfoDao.insert(classinfo);
        return classinfo;
    }

    /**
     * 修改数据
     *
     * @param classinfo 实例对象
     * @return 实例对象
     */
    @Override
    public ClassInfo update(ClassInfo classinfo) {
        this.classinfoDao.updateByPrimaryKey(classinfo);
        return this.queryById(classinfo.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.classinfoDao.deleteByPrimaryKey(id) > 0;
    }
}
